/*
 * Copyright (C) 2012-2013  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.remote.discovery;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import nemos.remote.util.EndpointProvider;
import nemos.remote.util.RemoteUtil;

import org.osgi.framework.ServiceReference;
import org.osgi.service.remoteserviceadmin.EndpointDescription;
import org.osgi.service.remoteserviceadmin.RemoteConstants;

/**
 * RegistryImpl : The {@link IRegistry} simple implementation.
 * 
 * @author Romain DUBOIS
 */
public class SimpleRegistry extends EndpointProvider<IEndpointDispatcher> implements IRegistry {

    /**
     * Constructor
     */
    public SimpleRegistry() {
        super();
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.remote.discovery.IRegistry#register(java.util.Map)
     */
    @Override
    public void register(final Map<String, ?> pEndpoint) {
        if (pEndpoint != null) {
            notifyListeners(new EndpointDescription(pEndpoint), true);
        }
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.remote.discovery.IRegistry#unregister(java.util.Map)
     */
    @Override
    public void unregister(final Map<String, ?> pEndpoint) {
        if (pEndpoint != null) {
            notifyListeners(new EndpointDescription(pEndpoint), false);
        }
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.remote.util.EndpointProvider#getListenerType()
     */
    @Override
    protected Class<IEndpointDispatcher> getListenerType() {
        return IEndpointDispatcher.class;
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.remote.util.EndpointProvider#getScope(org.osgi.framework.ServiceReference)
     */
    @Override
    protected Collection<String> getScope(final ServiceReference<IEndpointDispatcher> pReference) {
        String lFilter;
        final String lFrameworkUuid = (String) pReference
                .getProperty(RemoteConstants.ENDPOINT_FRAMEWORK_UUID);
        if (lFrameworkUuid == null) {
            lFilter = RemoteUtil.getRemoteEndpointFilter();
        } else {
            lFilter = RemoteUtil.getExcludingEndpointFilter(lFrameworkUuid);
        }

        // A dispatcher is not interested in services from its framework
        return Collections.singleton(lFilter);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.remote.util.EndpointProvider#notifyEndpoint(java.lang.Object,
     *      org.osgi.service.remoteserviceadmin.EndpointDescription,
     *      java.lang.String, boolean)
     */
    @Override
    protected void notifyEndpoint(final IEndpointDispatcher pListener,
            final EndpointDescription pEndpoint, @SuppressWarnings("unused") final String pFilter,
            final boolean pAdded) {
        if (pAdded) {
            pListener.dispatch(pEndpoint);
        } else {
            pListener.cancel(pEndpoint);
        }
    }
}
